import cv2
# from fontTools.ufoLib import fontInfoOpenTypeOS2CodePageRangesOptions
from util import camera
from util import public_tools as tool
from service import hr_service as hr

ADMIN_LOGIN = True


hr.load_emp_data()

tital = """
    **********************************************
    ******        MR智能视频打卡系统           ******
    **********************************************"""

# 启动方法
def start():
    finish = False  # 程序结束标志
    menu = """
    +---------------------------------------------+
    |                  主功能菜单                   |
    +---------------------------------------------+
    ◉打卡   ◉查看记录   ◉员工管理   ◉考勤报表   ◉退出
    +---------------------------------------------+"""
    while not finish:
        print(menu)  # 打印菜单
        option = input("请输入菜单序号：")
        if option == "1":  # 如果选择“打卡”
            face_clock()  # 启动人脸打卡
        elif option == "2":  # 如果选择“查看记录”
            if ADMIN_LOGIN:  # 如果管理员已登录
                check_record()  # 进入查看记录方法
            else:
                login()  # 先让管理员登录
        elif option == "3":  # 如果选择“员工管理”
            if ADMIN_LOGIN:
                employee_management()
            else:
                login()
        elif option == "4":
            if ADMIN_LOGIN:
                check_report()
            else:
                login()
        elif option == "5":
            finish = True
        else:
            print("输入的指令有误，请重新输入！")
    print("Bye Bye!")

#人脸打卡
def face_clock():
    """
    人脸打卡
    :return: None
    """
    print("请正对摄像头进行打卡")
    name = camera.clock_in()
    if name is not None:
        hr.add_lock_record(name)
        print(f"{name}打卡成功!")
    else:
        print("非员工，打卡失败！")


# 员工管理
def employee_management():
    menu = """ 
    +---------------------------------------------+
    |               员工管理功能菜单                 |
    +---------------------------------------------+
    ○录入新员工   ○删除员工   ○查看员工   ○返回上级菜单
    +---------------------------------------------+"""
    while True:
        print(menu)  # 打印菜单
        option = input("请输入菜单序号：")
        if option == "1":  # 如果选择“录入新员工”
            name = str(input("请输入新员工名称(输入0取消操作):")).strip()
            if name != "0":
                while hr.check_duplicate_name(name):
                    print(f"{name}已存在！")
                    print("是否确认使用重复名称(确认按1)")
                    choice = int(input())
                    if choice != 1:
                        name = str(input("请输入新员工名称(输入0取消操作):")).strip()
                    else:
                        break
                code = hr.add_new_employee(name)
                print("请面对摄像头，按3次Enter键完成拍照！")
                camera.register(code)
                print("录入成功！")
                return
        elif option == "2":
            print(hr.get_employee_report())
            id = int(input("输入要删除的员工编号(输入0取消操作):"))
            if id > 0:
                if hr.check_id(id):
                    verification = tool.randomNumber(4)
                    inputVer = input("[" + str(verification) + "] 请输入验证码：")
                    if str(verification) == str(inputVer).strip():
                        hr.remove_employee(str(id))
                        print(str(id) + "号员工已删除！")
                    else:
                        print("验证码错误！")
                else:
                    print("无此员工，操作取消")
        elif option == "3":
            hr.show_employee_all()
        elif option == "4":
            return
        else:
            print("输入有误，请重试输入！")


def check_record():
    menu = """ 
    +---------------------------------------------+
    |               查看记录功能菜单                 |
    +---------------------------------------------+
     ○查看员工列表      ○查看打卡记录      ○返回上级菜单
    +---------------------------------------------+"""
    while True:
        print(menu)  # 打印菜单
        option = input("请输入菜单序号：")
        if option == "1":  # 如果选择“打卡”
            print(hr.get_employee_report())
        elif option == "2":  # 如果选择“查看打卡记录”
            report = hr.get_record_all()
            print(report)
        elif option == "3":
            return
        else:
            print("输入有误，请重试输入！")


def check_report():
    menu = """ 
    +---------------------------------------------+
    |               考勤报表功能菜单                 |
    +---------------------------------------------+
     ○日报      ○月报      ○报表设置     ○返回上级菜单
    +---------------------------------------------+"""
    while True:
        print(menu)  # 打印菜单
        option = input("请输入菜单序号：")
        if option == "1":  # 如果选择“日报”
            while True:
                data = input("输入查询日期，格式为（2025-07-08）,输入0查询今天：")
                if data == "0":
                    hr.get_today_report()
                    break
                elif tool.valid_data(data):
                    hr.get_day_report(data)
                    break
                else:
                    print("日期格式有误，请重试！")
        elif option == "2":  # 如果选择“月报”
            while True:
                data = input("输入查询月份，格式为（2025-07）,输入0查询上月：")
                if data == "0":
                    hr.get_pre_month_report()
                    break
                elif tool.valid_year_month(data):
                    hr.get_month_report(data)
                    break
                else:
                    print("日期格式有误，请重试！")
        elif option == "3":
            report_config()
        elif option == "4":
            return
        else:
            print("输入有误，请重试输入！")


def report_config():
    menu = """ 
    +---------------------------------------------+
    |               报表设置功能菜单                 |
    +---------------------------------------------+
         ○作息时间设置            ○返回上级菜单
    +---------------------------------------------+"""
    while True:
        print(menu)  # 打印菜单
        option = input("请输入菜单序号：")
        if option == "1":  # 如果选择“日报”
            while True:
                work_time = input("请设置上班时间，格式（09:00:00）:")
                if tool.valid_time(work_time):
                    break
                else:
                    print("上班时间格式有误，请重试！")
            while True:
                close_time = input("请设置下班时间，格式（17:00:00）:")
                if tool.valid_time(close_time):
                    break
                else:
                    print("下班时间格式有误，请重试！")
            hr.save_work_time(work_time, close_time)
            print(f"设置完成，上班时间:{work_time} ,下班时间：{close_time} ")
        elif option == "2":
            return
        else:
            print("输入有误，请重试输入！")



if __name__ == '__main__':
    print(tital)
    start()
    # employee_management()
    # check_record()
    # check_report()